package com.card.game.room;

import com.card.utils.Utils;

/**   
 * @Description: TODO(用一句话描述该文件做什么) 
 * @author siyunlong  
 * @date 2016年9月2日 下午1:25:12 
 * @version V1.0   
 */
public class CardWallUtils {
	public static class CardWall{
		int index;
		int size;
		int gang;
		public CardWall(int index,int size) {
			this.index = index;
			this.size = size;
		}
		@Override
		public String toString() {
			return "CardWall [index=" + index + ", size=" + size + ", gang=" + gang + "]";
		}
	}
	
//	private static CardWall[] calCardWall2(boolean isNofeng,int lastSize,int sieve,int gang) {
//		int oldLastSize = lastSize;
//		CardWall[] walls = new CardWall[4];
//		int next = Utils.getFirstIndex(sieve);
//		for (int i = 0; i < 4; i++) {
//			walls[i] = new CardWall(next);
//			next = next == 3 ? 0 : next + 1;
//		}
//		int sumSize = isNofeng?120:136;
//		int avr = sumSize/4;
//		int geSize = sieve*2;
//		if(lastSize+gang <= geSize) {
//			CardWall first = walls[0];
//			first.size = lastSize;
//			first.gang = gang;
//			return walls;
//		}
//		CardWall first = walls[0];
//		first.size = Math.min(Math.max(geSize - gang, 0), lastSize);
//		first.gang = Math.min(geSize, gang);
//		gang = Math.max(0, gang-first.gang);
//		
//		lastSize = Math.max(lastSize - first.size, 0);
//		for (int i = 3; i >= 0; i--) {
//			CardWall cw = walls[i];
//			cw.size = Math.min(lastSize, avr);
//			lastSize -= cw.size;
//			if(lastSize <= 0) {
//				break;
//			}
//		}
//		if(gang > 0) {
//			for (int i = 3; i >= 0; i--) {
//				CardWall end = walls[3];
//				end.size -= gang;
//				end.gang = gang;
//			}
//		}
//		for (int i = 2; i >= 0; i--) {
//			CardWall cw = walls[i];
//			if(cw.size<avr) {
//				int oldSize = cw.size;
//				cw.size = Math.min(oldSize+gang, avr);
//				gang -= (cw.size-oldSize);
//			}
//		}
//		
//		int tempSize = 0;
//		for (int i = 3; i >= 0; i--) {
//			CardWall cw = walls[i];
//			tempSize += cw.size;
//		}
//		System.out.println("结果："+(tempSize==oldLastSize));
//		return walls;
//	}
	
	public static CardWall[] calCardWall(boolean isNofeng,int lastSize,int sieve,int gang) {
		if(isNofeng) {
			return calCardWallNoFeng(lastSize, sieve, gang);
		}
		return calCardWallHaveFeng(lastSize, sieve, gang);
	}
	
	private static CardWall[] calCardWallHaveFeng(int lastSize,int sieve,int gang) {
		int oldLastSize = lastSize;
		CardWall[] walls = new CardWall[4];
		int next = Utils.getFirstIndex(sieve);
		int sumSize = 136;
		int avr = sumSize/4;
		for (int i = 0; i < 4; i++) {
			walls[i] = new CardWall(next,avr);
			next = (next == 0 ? 3 : next - 1);
		}
		int chuSize = sumSize - lastSize;
		for (int i = 0; i < chuSize; i++) {
			CardWall wall = getFit(walls,sieve);
			wall.size--;
		}
		int fitGang = 0;
		for (int i = 0; i < gang; i++) {
			CardWall wall = getGangFit(walls);
			if(wall != null) {
				wall.size--;
				wall.gang++;
				fitGang++;
			}else{
				break;
			}
		}
		for (int i = 0; i < fitGang; i++) {
			CardWall wall = getAddFit(walls, sieve, gang, avr);
			wall.size++;
		}
		int tempSize = 0;
		for (int i = 3; i >= 0; i--) {
			CardWall cw = walls[i];
			tempSize += cw.size;
		}
		System.out.println("结果："+(tempSize==oldLastSize));
		
		return walls;
	}
	
	private static CardWall[] calCardWallNoFeng(int lastSize,int sieve,int gang) {
		int oldLastSize = lastSize;
		CardWall[] walls = new CardWall[4];
		int next = Utils.getFirstIndex(sieve);
		int zhuangIndex = next;
		int sumSize = 108;
		int avr = 26;
		for (int i = 0; i < 4; i++) {
			if(zhuangIndex == next) {
				avr = 30;
			}else{
				avr = 26;
			}
			walls[i] = new CardWall(next,avr);
			next = (next == 0 ? 3 : next - 1);
		}
		int chuSize = sumSize - lastSize;
		for (int i = 0; i < chuSize; i++) {
			CardWall wall = getFit(walls,sieve);
			wall.size--;
		}
		int fitGang = 0;
		for (int i = 0; i < gang; i++) {
			CardWall wall = getGangFit(walls);
			if(wall != null) {
				wall.size--;
				wall.gang++;
				fitGang++;
			}else{
				break;
			}
		}
		for (int i = 0; i < fitGang; i++) {
			CardWall wall = getAddFitNoFeng(walls, sieve, gang, avr,zhuangIndex);
			wall.size++;
		}
		int tempSize = 0;
		for (int i = 3; i >= 0; i--) {
			CardWall cw = walls[i];
			tempSize += cw.size;
		}
		System.out.println("结果："+(tempSize==oldLastSize));
		
		return walls;
	}
	
	private static CardWall getFit(CardWall[] walls,int sieve) {
		for (int i = 0; i < walls.length; i++) {
			int minNum = 0;
			if(i == 0) {
				minNum = sieve*2;
			}
			CardWall cardWall = walls[i];
			if(cardWall.size > minNum) {
				return cardWall;
			}
		}
		return walls[0];
	}
	
	private static CardWall getGangFit(CardWall[] walls) {
		CardWall frist = walls[0];
		if(frist.size>0) {
			return frist;
		}
		for (int i = walls.length-1; i >= 1 ; i--) {
			CardWall cardWall = walls[i];
			if(cardWall.size >0) {
				return cardWall;
			}
		}
		return null;
	}
	
	private static CardWall getAddFit(CardWall[] walls,int sieve,int gang,int avr) {
		CardWall frist = walls[0];
		if(frist.size < sieve*2-gang) {
			return frist;
		}
		int t = Math.min(sieve*2-gang, 0);
		for (int i = walls.length-1; i >= 1 ; i--) {
			CardWall cardWall = walls[i];
			if(cardWall.size < avr+t) {
				return cardWall;
			}
		}
		return frist;
	}
	
	private static CardWall getAddFitNoFeng(CardWall[] walls,int sieve,int gang,int avr,int zhuangIndex) {
		CardWall frist = walls[0];
		if(frist.size < sieve*2-gang) {
			return frist;
		}
		int t = Math.min(sieve*2-gang, 0);
		for (int i = walls.length-1; i >= 1 ; i--) {
			CardWall cardWall = walls[i];
			if(cardWall.index == zhuangIndex) {
				avr = 30;
			}else{
				avr = 26;
			}
			if(cardWall.size < avr+t) {
				return cardWall;
			}
		}
		return frist;
	}
	
	
	public static void main(String[] args) {
//		CardWall[] temp = calCardWallNoFeng(11, 2, 5);
//		for (CardWall cardWall : temp) {
//			System.out.println(cardWall);
//		}
//		System.out.println(GSONUtils.ToJSONString(temp));
		int next = 0;
		for (int i = 0; i < 4; i++) {
			System.out.println(next);
			next = (next == 0 ? 3 : next - 1);
		}
	}
}
